package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;


public class ThreeInputDialog extends Dialog implements View.OnClickListener {
    private Button btnCancel, btnConfirm;
    private TextView tvTitle;
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Context context;
    private ThreeDialogCallback callback;
    private int id;

    public ThreeInputDialog(@NonNull Context context, ThreeDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }

    public ThreeInputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View root = LayoutInflater.from(context).inflate(R.layout.three_input_dialog, null);
        btnCancel = root.findViewById(R.id.btn_cancel);
        btnConfirm = root.findViewById(R.id.btn_confirm);
        editText1 = root.findViewById(R.id.et_value_1);
        editText2 = root.findViewById(R.id.et_value_2);
        editText3 = root.findViewById(R.id.et_value_3);
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
    }


    public void showDialog(int id) {
        this.id=id;
        show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                callback.threeDialogInputResult(getValue(editText1),getValue(editText2),getValue(editText3),id);
                dismiss();
                clearData();
                break;
            case R.id.btn_cancel:
                dismiss();
                clearData();
                break;
            default:
        }
    }

    private void clearData(){
        editText1.setText("");
        editText2.setText("");
        editText3.setText("");
    }

    public void setHintMsg(String ms1,String ms2,String ms3){
        editText1.setHint(ms1);
        editText2.setHint(ms2);
        editText3.setHint(ms3);
    }

    private int getValue(EditText et) {
        int temp = 0;
        String ss = et.getText().toString().trim();
        if (!TextUtils.isEmpty(ss)) {
            temp = Integer.parseInt(ss);
        }
        return temp;
    }

    public interface ThreeDialogCallback {
        void threeDialogInputResult(int value1, int value2,int value3, int id);
    }
}
